Thread: What's the problem with my code? [Simple C for loops and 2d int]

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    4

    What's the problem with my code? [Simple C for loops and 2d int]

    Hello, I'm new to C language, and I'm using Visual C as my compiler..
    Here's my code:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <Windows.h>
    
    
        int i[8][8];
    void main(){
        int row;
        int col;
       for(row=0;row<=7;row++)
        {
            for(col=0;col<=7;col++)
            {
                i[row][col]=0;
            }
        }
       printi();
     
        getchar();getchar();
    }
    
    
        
    int printi()
    {
                char s[500];
        int row;
        int col;
        int g;
        int a=0;
           for ( row=0; row < 7; row++ ) 
           {
          for ( col=0; col < 7; col++ ) 
          {
             s[a]=("%c", (char)i[row][col] );
             a++;
          }
           }
    
    
           for(g=0;g<=sizeof(s);g++)
           {
               int a=0;
               if(a<8){
               printf("%c",s[g]);
               printf(" ");
               }
               else{
                   a=0;
                   printf("\n");
               }
           }
           return 0;
    }
    I want it to give this output:
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    But it gives me a weird set of characters that are not letters or numbers.. only weird symbols.. can u fix the code for me please and explain the problem?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    34
    Quote Originally Posted by waseem1345 View Post
    Hello, I'm new to C language, and I'm using Visual C as my compiler..
    Here's my code:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <Windows.h>
    
    
        int i[8][8];
    void main(){
        int row;
        int col;
       for(row=0;row<=7;row++)
        {
            for(col=0;col<=7;col++)
            {
                i[row][col]=0;
            }
        }
       printi();
     
        getchar();getchar();
    }
    
    
        
    int printi()
    {
                char s[500];
        int row;
        int col;
        int g;
        int a=0;
           for ( row=0; row < 7; row++ ) 
           {
          for ( col=0; col < 7; col++ ) 
          {
             s[a]=("%c", (char)i[row][col] );
             a++;
          }
           }
    
    
           for(g=0;g<=sizeof(s);g++)
           {
               int a=0;
               if(a<8){
               printf("%c",s[g]);
               printf(" ");
               }
               else{
                   a=0;
                   printf("\n");
               }
           }
           return 0;
    }
    I want it to give this output:
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    But it gives me a weird set of characters that are not letters or numbers.. only weird symbols.. can u fix the code for me please and explain the problem?

    Thanks in advance
    Erm, not really sure what you're trying to do. but if you're goal is to just display a bunch of 0's (8 across, 8 deep), your code is a little confusing.

    I adjusted your code slightly, you should be able to see the difference.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void printi(int row, int col, int i[row][col]);
    
    int main() 
    {
        int i[8][8];
    
        int row;
        int col;
    
       for(row = 0; row < 8; row++)
        {
            for(col = 0; col < 8; col++)
            {
                i[row][col] = 0;
            }
        }
        printi(row, col, i);
        
        return 0;
    }
    
    void printi(int row, int col, int i[row][col])
    {
           for (row = 0; row < 8; row++ ) 
           {
              for (col = 0; col < 8; col++) 
              {
                  printf("%d", i[row][col]);
              }
              printf("\n");
           }
    }
    if this doesn't answer your question, let me know.

    Also, a little note for you, if you're using visual studio, use 'start without debugging' and you won't need to add 'getchar()' to stop the window from closing
    Last edited by Lawson; 12-17-2015 at 08:02 PM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Where to begin...
    Code:
     s[a]=("%c", (char)i[row][col] );
    This is a rather complex way to assign irow,col as a char to s[a]. "%c" has no effect thanks to the comma operator. (Yes, the comma is an operator here.)

    The crux of your problem is here:
    Code:
    for(g=0;g<=sizeof(s);g++)
    I think if you print the value of sizeof(s) you will find it is much greater than 8x8.

    This explains your results because most of the unused space in s is uninitialized. Variables are not zeroed out for you in C unless they are in global scope. (Ironic, given the scope of i). Now that you know that, please do not make all of your variables global from now on, but make sure that variables have sensible values before you use them. Using uninitialized variables leads to undefined behavior and bugs.

    In the case of arrays, simply zeroing them out is sufficient.
    Code:
     char s[500] = {0};
    There are a couple ways to declare arrays, but if your array has a supplied size, as it does here, you do not need to supply all of the initial values; they will be zeroed out for you.
    Code:
     char t[] = { 0, 0, 0, 0, 0 };
    In contrast, t has an inferred size, and the number of initial values is the size of the array.

    Also, the char type will not display 0 as a zero. '0' is the digit zero. 0 or '\0' -- the character -- is a null. Unless you have some specific reason, printing the values of i with printf("%d ", i[row][col]); is probably the easiest way.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    Also posted here.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    4
    Thanks a lot!
    This fixed my code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  2. while loops...a problem with my simple code?
    By niceguy in forum C Programming
    Replies: 13
    Last Post: 02-20-2008, 02:59 PM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 06:27 AM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM

Tags for this Thread